<?php
/*
 * 手机客户端 专场数据接口
 * 2013-11-27
 */
 
 
 /**
  * 专场列表
  * 传入参数
  * 			$type  类型 
  * 							0预展（按预展开始时间顺序排列） 
  *								1竞价（按竞价开始时间倒序排列）
  * 							2成交（按竞价结束时间倒序排列）
  *								空参数 全部专场数据，按预展开始倒序排列
  *
  * 			$page 分页		0为第一页
  *
  * 			$pageSize 每页记录数
  *				$category		分类 1瓷器  2黑茶
  */
 function special_list($type,$page,$pageSize,$category){
 
	//图片根路径
	$img_basePath = "http://www.chinalaohu.com/modules/miniature.php?w=150&h=150&p=/uploads/300*300/";
	
	if(!isset($page)){
		$page = 0;
	}

	if(!isset($pageSize)){
		$pageSize = 10;
	}
	
	$reArrl = array(); //构建返回值

	$where = ""; 	//查询条件语句
	$orderBy = "";	//排序条件
	if($type=="0"){
		//预展期间
		$where = " where a.astag='1' and now()<a.asprevclosetime ";
		$orderBy = " order by a.asprevtime asc ";
	}else if($type=="1"){
		//竞价期间
		$where = " where a.astag='1' and now() between a.asopentime and a.asclosetime ";
		$orderBy = " order by a.asopentime desc ";
	}else if($type=="2"){
		//成交
		$where = " where a.astag='1' and now()>a.asclosetime ";
		$orderBy = " order by a.asclosetime desc ";
	}else{
		//全部
		$where = " where a.astag='1' and now()>a.asprevtime ";
		$orderBy = " order by a.asprevtime desc ";
	}
	if($category){
		$where = $where." and a.pcid='".$category."' ";
	}
	
	//查询记录总数
	$sql = "select count(0) as ct from auction_special a ".$where;
	$db = $GLOBALS['db'];
	$db->query($sql);
	if($db->next_record()){
		$reArrl["count"] = $db->f("ct");
	}else{
		$reArrl["count"] = "0";
		return $reArrl;
	}
	
	//专场类别
	if($page==0){
		$categorys[] = array(
		"name"=>"全部"											//专场类别
		,"id"=>""										//专场类别id

	);
	$categorys[] = array(
		"name"=>"新中国瓷器"											//专场类别
		,"id"=>"1"										//专场类别id

	);
	$categorys[] = array(
		"name"=>"白沙溪黑茶"											//专场类别
		,"id"=>"2"										//专场类别id
	);
	$categorys[] = array(
		"name"=>"大学生专场"											//专场类别
		,"id"=>"3"										//专场类别id
	);
	$categorys[] = array(
		"name"=>"日用瓷器"											//专场类别
		,"id"=>"4"										//专场类别id
	);
	$reArrl["category"] = $categorys;
	}
	

	$sql = "select 
					a.asid
					,a.aspecial
					,if((now() < a.asprevclosetime),0,if((now() between a.asopentime and a.asclosetime),1,2)) as type
					,if((now() < a.asprevclosetime),date_format(a.asprevtime,'%Y-%m-%d %H:%i:%s'),date_format(a.asopentime,'%Y-%m-%d %H:%i:%s')) as stime
					,if((now() < a.asprevclosetime),date_format(a.asprevclosetime,'%Y-%m-%d %H:%i:%s'),date_format(a.asclosetime,'%Y-%m-%d %H:%i:%s')) as etime
					,if((now() < a.asprevclosetime),date_format(a.asprevtime,'%m'),date_format(a.asopentime,'%m')) as cmonth
					,b.ct as auctionCount
					,c.ct as auctionTimes
					,a.astopic_n
			from 
					auction_special a
					left join (select count(aid) as ct,asid from auction group by asid) b on a.asid=b.asid 
					left join (select count(a.aubid) as ct,c.asid from auction_userbid a  inner join auction b on a.aid=b.aid inner join auction_special c on b.asid=c.asid group by c.asid) c on a.asid=c.asid
			".$where.$orderBy." limit ".($page*$pageSize).",".$pageSize;
			
	$db->query($sql);
	
	$list = array(); //记录集数组
	$reArrl["list"] = &$list;
	
	while($db->next_record()){
		$auctionCount = $db->f("auctionCount");
		if(!$auctionCount){
			$auctionCount = "0";
		}
		$auctionTimes = $db->f("auctionTimes");
		if(!$auctionTimes){
			$auctionTimes = "0";
		}
		$list[] = array(
			 "asid"=>$db->f("asid")
			,"img"=>$img_basePath.$db->f("astopic_n")
			//,"img"=>$img_basePath.$db->f("asid").".jpg"
			,"title"=>trim($db->f("aspecial"))
			,"type"=>$db->f("type")
			,"stime"=>$db->f("stime")
			,"etime"=>$db->f("etime")
			,"auctionCount"=>$auctionCount
			,"auctionTimes"=>$auctionTimes
			,"month"=>$db->f("cmonth")
		);
	}
	return $reArrl;
 }
 ?>